import dashscope,json
from django.http import HttpResponse
from dashscope.audio.tts_v2 import VoiceEnrollmentService
from django.conf import settings

dashscope.api_key = settings.ALIYUN_KEY

url = "https://chat.askok.cn/b.mp3"  # 请按实际情况进行替换
prefix = '002'
target_model = "cosyvoice-clone-v1"


def create_voice(request):
    try:
        service = VoiceEnrollmentService()
        voice_id = service.create_voice(target_model=target_model, prefix=prefix, url=url)
        return HttpResponse(json.dumps({'code': 0,'msg':'ok', 'voice_id': f"{voice_id}"}))
    except Exception as e:
        return HttpResponse(json.dumps({'code': 1, 'msg': f"{e}"}))
def update_voice(req):
    service = VoiceEnrollmentService()
    voice_id = req.GET.get('voice_id','')
    try:
        url = req.GET.get('url','')
        service.update_voice(voice_id,url)
        return HttpResponse(json.dumps({"code":0,"msg":"ok"}))
    except Exception as e:
        return HttpResponse(json.dumps({'code': 1, 'msg': f"{e}"}))